package com.aco.fatureapp.auxiliary;

import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import br.com.ac.fatureweb.integration.utilitary.NumberUtils;

import com.aco.fatureapp.R;
import com.aco.fatureapp.model.ProductDetail;

public class ShowBillAdapter extends BaseExpandableListAdapter {

	private Context mContext;
	private List<ShowBillItem> mListData;
	private HashMap<Integer, List<ProductDetail>> mListDataChild;

	public ShowBillAdapter(Context context, List<ShowBillItem> listDataHeader,
			HashMap<Integer, List<ProductDetail>> listChildData) {
		this.mContext = context;
		this.mListData = listDataHeader;
		this.mListDataChild = listChildData;
	}

	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return this.mListDataChild.get(groupPosition) != null ? this.mListDataChild
				.get(groupPosition).get(childPosititon) : null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	@SuppressLint("InflateParams")
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		final ProductDetail detail = (ProductDetail) getChild(groupPosition,
				childPosition);

		LayoutInflater infalInflater = (LayoutInflater) this.mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (detail != null) {
			convertView = infalInflater.inflate(
					R.layout.adapter_show_bill_detail, null);

			TextView txtDscProduto = (TextView) convertView
					.findViewById(R.id.txtDscProduto);

			TextView txtLeituraAnterior = (TextView) convertView
					.findViewById(R.id.txtLeituraAnterior);

			TextView txtLeituraAtual = (TextView) convertView
					.findViewById(R.id.txtLeituraAtual);

			TextView txtConsumoMedido = (TextView) convertView
					.findViewById(R.id.txtConsumoMedido);

			TextView txtConsumoFaturado = (TextView) convertView
					.findViewById(R.id.txtConsumoFaturado);

			TextView txtConsumoAgua = (TextView) convertView
					.findViewById(R.id.txtConsumoAgua);

			TextView txtVlrFaturado = (TextView) convertView
					.findViewById(R.id.txtVlrFaturado);

			TextView txtOcorrencia = (TextView) convertView
					.findViewById(R.id.txtOcorrencia);

			if (detail.vlrConsumoAgua != null) {
				txtConsumoAgua.setText(String.valueOf(detail.vlrConsumoAgua));
			}

			if (detail.vlrConsumoMedido != null) {
				txtConsumoMedido.setText(String
						.valueOf(detail.vlrConsumoMedido));
			}

			if (detail.vlrConsumoFaturado != null) {
				txtConsumoFaturado.setText(String
						.valueOf(detail.vlrConsumoFaturado));
			}

			if (detail.vlrLeituraAnterior != null) {
				txtLeituraAnterior.setText(String
						.valueOf(detail.vlrLeituraAnterior));
			}

			if (detail.vlrLeituraAtual != null) {
				txtLeituraAtual.setText(String.valueOf(detail.vlrLeituraAtual));
			}

			txtOcorrencia.setText(detail.dscOcorrencia);
			txtDscProduto.setText(detail.dscProduto);

			if (detail.vlrFaturado != null) {
				txtVlrFaturado.setText(NumberUtils
						.formatMoney(detail.vlrFaturado));
			}
			else {
				txtVlrFaturado.setText("0.0");
			}
		}
		else {
			convertView = infalInflater.inflate(
					R.layout.adapter_show_bill_detail_empty, null);
		}

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return this.mListDataChild.get(groupPosition) != null ? this.mListDataChild
				.get(groupPosition).size() : 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return this.mListData.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return this.mListData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	@SuppressLint("InflateParams")
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ShowBillItem item = (ShowBillItem) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) this.mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(
					R.layout.adapter_show_bill_group, null);
		}

		TextView txtDescricao = (TextView) convertView
				.findViewById(R.id.txtDescricao);

		TextView txtParcela = (TextView) convertView
				.findViewById(R.id.txtParcela);

		TextView txtValor = (TextView) convertView.findViewById(R.id.txtValor);

		txtDescricao.setText(item.getDescricao());
		txtParcela.setText(item.getParcela());

		if (item.getValor() != null) {
			txtValor.setText(NumberUtils.formatMoney(item.getValor()));
		}
		else {
			txtValor.setText("0.0");
		}

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}